﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Blog.Models;

namespace Blog.Helper
{
    public class PagetedList<T>:List<T>
    {
        public int PageIndex   { get; private set; }
        public int PageSize    { get; private set; }
        public int TotalCount  { get; private set; }
        public int TotalPages  { get; private set; }
        public PagetedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);
            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }
        public bool HasPreviousPage {
            get {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage {
            get {
                return (PageIndex+1 < TotalPages);
            }
        }
    }
    public static class Helper
    {
        public static bool IsImage(string inputImage)
        {
            if (inputImage == null | inputImage.Length < 4) return false;
            var strRegex = @"(.\.(gif|jpg|png|tif|bmp|ico|emf|wmf|jpeg))";
            var re = new Regex(strRegex);
            return (re.IsMatch(inputImage));
        }
    }
}
